var Api = require('../../utils/api.js');
var app = getApp();
var imageUtil = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
var can_submit = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView: true,
    flag: true,
    content: ''
  },
  onLoad: function (options) {
    var that = this;
    // 生命周期函数--监听页面加载
    app.getUserInfo(function (userInfo) {
      var user = wx.getStorageSync('user');
      var url = Api.HOST + 'wechat.php?s=Home/order';
      
      var data = {};
      data.nickname = userInfo.nickName;
      data.headimgurl = userInfo.avatarUrl;
      data.openid = user.openid;

      Api.fetchPost(url, data, (err, res) => {
        that.setData({
          content: res.content,
        });
        var article = res.content;
        WxParse.wxParse('article', 'html', article, that, 5);
      });
    })
  },
  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  a: function () {
    this.setData({ flag: false })
  },
  b: function () {
    this.setData({ flag: true })
  },
  //提交表单
  sendformSubmit: function (e) {
    if (can_submit == 1) {
      if (this.data.showView==false){
        Api.alert('请同意授权协议');
        return false;
      }
      if (e.detail.value.name == "") {
        Api.alert('请填写姓名');
        return false;
      }
      if (e.detail.value.people == "") {
        Api.alert('请填写身份证号');
        return false;
      }
      if (e.detail.value.phone == "") {
        Api.alert('请填写手机号');
        return false;
      }
      can_submit = 0;
      var that = this;
      var url = Api.HOST + 'wechat.php?s=Home/submit';
      var user = wx.getStorageSync('user');
      var data = {};
      data.openid = user.openid;
      data.name = e.detail.value.name;
      data.phone = e.detail.value.phone;
      data.people = e.detail.value.people;
      Api.fetchPost(url, data, (err, res) => {
        console.log(res);
        var id=res.id;
        if (res.ret == 1) {
          if(res.free == 0){
            var payargs = res.parameter;
            wx.requestPayment({
              timeStamp: payargs.timeStamp,
              nonceStr: payargs.nonceStr,
              package: payargs.package,
              signType: payargs.signType,
              paySign: payargs.paySign,
              success: function (res) {
                can_submit = 1;
                wx.redirectTo({
                  url: '/pages/result/result?id=' + id,
                })
              },
              fail: function (res) {
                can_submit = 1;
                wx.redirectTo({
                  url: '/pages/query/query',
                })
              }
            });
          }else{
            can_submit = 1;
            wx.redirectTo({
              url: '/pages/result/result?id=' + id,
            })
          }
          
        } else {
          Api.alert(res.msg, function () { can_submit = 1; return false })
        }

      });
    }
  },
  onShareAppMessage: function (res) {
    console.log(res)
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '大数据征信查询',
        path: '/pages/index/index',
        success: function (res) {
          // 转发成功
          var that = this;

          app.getUserInfo(function (userInfo) {
            var user = wx.getStorageSync('user');
            var url = Api.HOST + 'wechat.php?s=Home/share';

            var data = {};
            data.openid = user.openid;

            Api.fetchPost(url, data, (err, res) => {
              that.setData({

              });
            });
          })

        },
        fail: function (res) {
          // 转发失败
        }
      }
    } else {
      return {
        title: '大数据征信查询',
        path: '/pages/index/index',
        success: function (res) {
          // 转发成功
          var that = this;

          app.getUserInfo(function (userInfo) {
            var user = wx.getStorageSync('user');
            var url = Api.HOST + 'wechat.php?s=Home/share';

            var data = {};
            data.openid = user.openid;

            Api.fetchPost(url, data, (err, res) => {
              that.setData({

              });
            });
          })

        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  },

})